package controller

import (
	"fmt"
	"hft/Database/admin"
	services "hft/Http/Services"
	utils "hft/Http/Utils"
	"hft/tools"

	"github.com/gin-gonic/gin"
)

func (*Active) CreateGroup(c *gin.Context) {
	var group *admin.ActiveGroup
	c.ShouldBindJSON(&group)
	fmt.Println(tools.JsonParse(group))
	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
		return
	}

	err = services.ActiveService.CreateGroup(claims.BaseClaims, *group)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.Ok(c)
}

func (*Active) EditGroup(c *gin.Context) {
	var group *admin.ActiveGroup
	c.ShouldBindJSON(&group)
	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
		return
	}

	err = services.ActiveService.EditGroup(claims.BaseClaims, *group)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.Ok(c)
}

func (*Active) GetGroupList(c *gin.Context) {

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
		return
	}

	groups, err := services.ActiveService.GetGroupList(claims.BaseClaims)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.OkWithData(groups, c)

}
